extends StateBase


var player: Player
var stuck_timer: float = 0
var escape_frames = 0


func on_enter(owner = null, params = null):
	player = owner as Player
	
	stuck_timer = 0
	escape_frames = 0
	

func on_exit(owner = null):
	player.level.destroy_current_quick_switch_item_ui()
	player.coyote_timer = 0
	player.jump_buffer_timer = 0
	player.is_pushing_partner = false


func on_process(delta: float, owner = null):
	pass
	
	
func on_physics_process(delta: float, owner = null):
	var player_input = Input.get_axis("move_left", "move_right")
	if not player.enable_input:
		player_input = 0
		
	var player_prev_position = player.global_position
	
	player.process_coyote_time(delta)
	player.process_jump_buffer(delta)
	player.process_move_statemove(delta)
	player.process_enter_jump()
	player.process_gravity(delta)
	player.process_animation_statemove()
	player.process_vertical_move_viewroot(delta)
	player.process_use_item()
	player.process_pickup()
	player.process_quick_switch_item()
	player.process_open_ui_card()
	
	player.process_push_partner(delta)
	#player.process_will_collider_with_unit(delta)
	player.move_and_slide()
	
	if player.global_position.distance_to(player_prev_position) < 0.5 and abs(player_input) > 0.1:
		stuck_timer += delta
	else:
		stuck_timer = 0.0
		
	if stuck_timer > 0.3:
		try_escape()
		
	if escape_frames > 0:
		escape_frames -= 1
		var dir = Vector2(randf_range(-1,1), randf_range(-1,1)).normalized()
		player.move_and_collide(dir * 30 * delta)
	
	player.process_drop_item()
	player.process_exit_stuck(delta)
	player.process_gun_hand_rotation()
	

func try_escape():
	escape_frames = 5  # 抖动5帧

	
